﻿using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEditor.SceneManagement;

namespace Nirvana
{
    [CustomEditor(typeof(DefaultShaderRule), true)]
    public class DefaultShaderRuleEditor : Editor
    {
        private SerializedProperty default_material;
        private SerializedProperty default_particleSystem, default_particle;
        private SerializedProperty default_sprite;

        private static bool isHold1, isHold2;

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.default_material = serializedObject.FindProperty("default_material");
            this.default_particleSystem = serializedObject.FindProperty("default_particleSystem");
            this.default_particle = serializedObject.FindProperty("default_particle");
            this.default_sprite = serializedObject.FindProperty("default_sprite");
            
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            EditorGUILayout.HelpBox("\n此规则文件用于清除，替换Prefab中出现的内置Default材质球，避免创建多份Material，降低Shader.Parse性能消耗。\n" +
                "注意：\n" +
                "    打AB包时，最好以依赖方式打包，避免每个AB都包含一份Mat", MessageType.Info);
            EditorGUILayout.Space();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents();
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n", MessageType.None);
            GUI.color = Color.white;
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            EditorGUILayout.Space();
            isHold1 = EditorGUILayout.Foldout(isHold1, "Clear Prefab Default Material");
            if (isHold1)
            {
                isHold2 = false;
                EditorGUILayout.PropertyField(this.default_material, new GUIContent("Clear Default-Material"));
                EditorGUILayout.PropertyField(this.default_particleSystem, new GUIContent("Clear Default-ParticleSystem"));
                EditorGUILayout.PropertyField(this.default_sprite, new GUIContent("Clear Sprites-Default"));
                EditorGUILayout.Space();
                if (GUILayoutEx.ActionButton("清除默认材质球"))
                {

                    DefaultShaderRule rule = this.target as DefaultShaderRule;
                    rule.FixPrefabStandardShader(false);
                }
            }

            EditorGUILayout.Space();

            EditorGUILayout.Space();
            isHold2 = EditorGUILayout.Foldout(isHold2, "Replace Prefab Default Material");
            if (isHold2)
            {
                isHold1 = false;
                EditorGUILayout.PropertyField(this.default_material, new GUIContent("Replace Default-Material"));
                EditorGUILayout.PropertyField(this.default_particle, new GUIContent("Replace Default-Particle"));
                EditorGUILayout.PropertyField(this.default_particleSystem, new GUIContent("Replace Default-ParticleSystem"));
                EditorGUILayout.PropertyField(this.default_sprite, new GUIContent("Replace Sprites-Default"));

                EditorGUILayout.Space();
                if (GUILayoutEx.ActionButton("替换默认材质球"))
                {
                    DefaultShaderRule rule = this.target as DefaultShaderRule;
                    rule.FixPrefabStandardShader(true);
                    GUIUtility.ExitGUI();
   
                }
            }


           serializedObject.ApplyModifiedProperties();
        }
    }
}

